# Maintainer: He Yangxuan <yangxuan8282@gmail.com>

pkgname=linux-amlogic
pkgver=4.18.14
case $pkgver in
*.*.*)	_kernver=${pkgver%.*};;
*.*)	_kernver=${pkgver};;
esac
pkgrel=2
pkgdesc="Linux kernel for Amlogic"
url=https://github.com/torvalds/linux
_commit=e7d199e92956587695510d147c8de795f944cec9
depends="mkinitfs"
_depends_dev="perl gmp-dev elfutils-dev bash"
makedepends="$_depends_dev sed installkernel bc linux-headers linux-firmware openssl-dev bison flex"
options="!strip !check"
source="$url/archive/${_commit}.tar.gz
	config-changes-amlogic.aarch64
	defconfig
	0001-ARM64-dts-meson-gxbb-nanopi-k2-Add-HDMI-CEC-and-CVBS.patch
	0002-drm-meson-Make-DMT-timings-parameters-and-pixel-cloc.patch
	0003-ARM64-defconfig-enable-CEC-support.patch
	0004-clk-meson-switch-gxbb-cts-amclk-div-to-the-generic-d.patch
	0005-clk-meson-remove-unused-clk-audio-divider-driver.patch
	0006-ASoC-meson-add-meson-audio-core-driver.patch
	0007-ASoC-meson-add-register-definitions.patch
	0008-ASoC-meson-add-aiu-i2s-dma-support.patch
	0009-ASoC-meson-add-initial-i2s-dai-support.patch
	0010-ASoC-meson-add-aiu-spdif-dma-support.patch
	0011-ASoC-meson-add-initial-spdif-dai-support.patch
	0012-ARM64-defconfig-enable-audio-support-for-meson-SoCs-.patch
	0013-ARM64-dts-meson-gx-add-audio-controller-nodes.patch
	0014-snd-meson-activate-HDMI-audio-path.patch
	0015-drm-meson-select-dw-hdmi-i2s-audio-for-meson-hdmi.patch
	0016-ARM64-dts-meson-gx-add-sound-dai-cells-to-HDMI-node.patch
	0017-ARM64-dts-meson-activate-hdmi-audio-HDMI-enabled-boa.patch
	0018-drm-bridge-dw-hdmi-Use-AUTO-CTS-setup-mode-when-non-.patch
	0019-drm-meson-Call-drm_crtc_vblank_on-drm_crtc_vblank_of.patch
	0020-media-platform-meson-ao-cec-make-busy-TX-warning-sil.patch
	0021-soc-amlogic-add-meson-canvas-driver.patch
	0022-dt-bindings-soc-amlogic-add-meson-canvas-documentati.patch
	0023-ARM64-dts-meson-gx-add-dmcbus-and-canvas-nodes.patch
	0024-drm-meson-convert-to-the-new-canvas-module.patch
	0025-WIP-drm-meson-Support-Overlay-plane-for-video-render.patch
	0026-media-meson-add-v4l2-m2m-video-decoder-driver.patch
	0027-ARM64-dts-meson-gx-add-vdec-entry.patch
	0001-libretech-cc-disable-CVBS-connector.patch
	0001-ARM64-dts-meson-add-vdec-entries.patch
	add-phicomm-n1.patch
	bt-btbcm.patch
	brcmfmac-Disable-power-management.patch
	offset.patch
	off_error_text_offset.patch
	"
subpackages=""
arch="armhf armv7 aarch64"
license="GPL-2.0"
_flavors=
for _i in $source; do
	case $_i in
	config-*.$CARCH)
		_f=${_i%.$CARCH}
		_f=${_f#config-changes-}
		_flavors="$_flavors ${_f}"
		[ "linux-$_f" != "$pkgname" ] && subpackages="$subpackages linux-${_f}::$CBUILD_ARCH"
		subpackages="$subpackages linux-${_f}-dev:_dev:$CBUILD_ARCH"
		;;
	esac
done

case "$CARCH" in
	aarch64) _carch="arm64" ;;
	arm*) _carch="arm" ;;
esac

HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

prepare() {
	local _patch_failed=
	cd "$srcdir"/linux-$_commit

	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/${i##*/}; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# remove localversion from patch if any
	rm -f localversion*

	local flavor=
	for flavor in $_flavors; do
		local builddir="$srcdir"/build-$flavor
		mkdir -p "$builddir"
		echo "-$pkgrel-$flavor" > "$builddir"/localversion-alpine
		_genconfig $flavor
		make -C "$srcdir"/linux-$_commit \
			O="$builddir" \
			ARCH="$_carch" \
			HOSTCC="$HOSTCC" \
			olddefconfig
		_verifyconfig $flavor
	done
}

# generate config from defconfig and apply local changes.
# config-changes-$flavor.$CARCH holds a list of = delimited
# config command and values used by kernel scripts/config script.
_genconfig() {
	local flavor=$1 defconfig=
	local builddir="$srcdir"/build-$flavor
	local defconfig=
	case $flavor in
		amlogic) defconfig=defconfig ;;
		*) die "Unknown flavor: $flavor" ;;
	esac

	cp "$srcdir"/$defconfig \
		"$builddir"/.config

	while read line; do
		[ ${line:0:1} = "#" ] && continue
		local option=${line%%=*} str=
		local cmd=$(echo $line | cut -d= -f2)
		case "$cmd" in
			enable|disable|module) str= ;;
			set-str|set-val) str=${line##*=} ;;
			*) die "Command $cmd not accepted" ;;
		esac
		msg "[$flavor] $cmd: $option $str"
		"$srcdir"/linux-$_commit/scripts/config \
			--file "$builddir"/.config \
			--${cmd} "$option" "${str//\"/}"
	done < "$srcdir"/config-changes-$flavor.${CARCH}
}

# verify if options are set to correct value
_verifyconfig() {
	local flavor=$1
	local builddir="$srcdir"/build-$flavor
	while read line; do
		[ ${line:0:1} = "#" ] && continue
		local option=${line%%=*} str= invert=
		local cmd=$(echo $line | cut -d= -f2)
		case "$cmd" in
			enable) str="$option=y" ;;
			disable) str="$option"; invert="-v" ;;
			module) str="$option=m" ;;
			set-val) str="$option=${line##*=}" ;;
			set-str) str=${line##*=}
				str="$option=\"${str//\"/}\"" ;;
		esac
		grep -q $invert "^$str" "$builddir"/.config || \
			die "Config: $option not properly set!"
	done < "$srcdir"/config-changes-$flavor.${CARCH}
}

build() {
	unset LDFLAGS
	for i in $_flavors; do
		cd "$srcdir"/build-$i
		make ARCH="$_carch" CC="${CC:-gcc}" \
			KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	done
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	local _abi_release=${pkgver}-${pkgrel}-${_buildflavor}

	cd "$srcdir"/build-$_buildflavor

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	local _install
	case "$CARCH" in
	arm*)
		_install="zinstall dtbs_install"
		;;
	aarch64)
		_install="install dtbs_install"
		;;
	*)
		_install=install
		;;
	esac

	cd "$srcdir"/build-$_buildflavor
	local INSTALL_DTBS_PATH="$_outdir"/boot/dtbs
	make -j1 modules_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$INSTALL_DTBS_PATH"

	rm -f "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf "$_outdir"/lib/firmware

	install -D include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release

	if [ "$CARCH" = "aarch64" ]; then
		mv -f "$INSTALL_DTBS_PATH"/amlogic/*.dtb \
			"$INSTALL_DTBS_PATH"
		rmdir "$INSTALL_DTBS_PATH"/amlogic
	fi
}

# main flavor installs in $pkgdir
package() {
	depends="$depends linux-firmware-brcm"
	_package amlogic "$pkgdir"
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local _abi_release=${pkgver}-${pkgrel}-$_flavor
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/build-$_flavor/.config "$dir"/.config
	make -j1 -C "$srcdir"/linux-$_commit O="$dir" HOSTCC="${CC:-gcc}" \
		silentoldconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$srcdir"/linux-$_commit
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"
	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build-$_flavor/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="b65dc14801b987b4d8ca786aa6571da12fd929ab147ed2f71f366b32313cd72d3047830e502b3fe51f9f1f3963d46b7f5b45ed937a4bdfee14dc216b32de2d3a  e7d199e92956587695510d147c8de795f944cec9.tar.gz
7186849193386fc06caab6db9ebf3d5b7efe2511f1c3836bb70d713340eb69b1f123f8372f15ee7f8dd61eeff5ce772b35db8557133ef64a2a1a5ca9e7cd30c9  config-changes-amlogic.aarch64
2784f8c05c1c66f2bff73d0cdd415781e6529f269223607c18302803125092e7cb6c94083146deecc3226672f7f4754246cc62808f9384f697782ec55786883d  defconfig
94f944448156e435030d84d787d1489cdab4a6733b1fd27cd4ecd1284337d2b3e68bb3f74b3268a4cac01587e96019d4ad2a354c59d1422b6caae3f97849717f  0001-ARM64-dts-meson-gxbb-nanopi-k2-Add-HDMI-CEC-and-CVBS.patch
997fee925f07735e635fdd2d2474500ca5a81a80a7817c079174e2508eec223264c97b79cfc057d6b4e428c4d9392405f79befd242dc9b21e020f544ffd84c9d  0002-drm-meson-Make-DMT-timings-parameters-and-pixel-cloc.patch
3294e40073360e9f6daf3360022a8443bdab960ba0f261e5858e36e7b838af0304888eef9992ef93f67a5dc97f0a26c46f08da1c2f32ca1fe01b9754f55ae975  0003-ARM64-defconfig-enable-CEC-support.patch
02c5a7b99be3a180eb40716735ced4ba5f9da1897cb9132c3376b660ba1c005df60b9da1ad7808f53027b7d37b89ef007c71aa2fdfd5ad77be4425886348c3b2  0004-clk-meson-switch-gxbb-cts-amclk-div-to-the-generic-d.patch
8d4d3bead45b420b0920eb4eb77fc938c2bcb64a3304d5690432aec7a235f348deba4246b737742901102f256313ab34f95caa8932184099ed20baaf23913f6f  0005-clk-meson-remove-unused-clk-audio-divider-driver.patch
7057d93953fe9bb83415ac82fe63bacc64161f295e1340dc8b56330a74655c44e9f7feb621f12b74e7d92497115abd918e7b087bc0ece7d0322e384fbd5acd95  0006-ASoC-meson-add-meson-audio-core-driver.patch
42ba176a6198fae2146f871dc469fb94b40382ff966b167f976d5e28375f4849bda6141b01e786d37cf14ca71ba159ee0a0316ec477150040517cebf0f638334  0007-ASoC-meson-add-register-definitions.patch
720c21f11514edba0164ecfe260b8d2409ccea5eaaf4d98013e42a7e4f160fbe30d6a25fd94ae1fae115742fff298ebab1a079ac1f54ae0dc39cfb5567eb693c  0008-ASoC-meson-add-aiu-i2s-dma-support.patch
19d3afeae370201d513c98137bd3833ec3bb8fbd43dee5a391a6e4926a31b381746725e3abdc907a2f24f72ae6757687083d0b99ba1e27a34d90e0f06e506573  0009-ASoC-meson-add-initial-i2s-dai-support.patch
d9da5fc3cf9d6942aa995c06e6c22bfeac89add9dc7d0dd4f58cb4d8e07ceb3679d8af1dd68ba1510bc964cc6303cdf38954b7364ccd9bf22de4c379f12cfbe3  0010-ASoC-meson-add-aiu-spdif-dma-support.patch
9cbab78248da16985ac64c3ee58652df36ae3a8313d7723a168ed36cb23c6d488dd21a603b09ac2fc7ee88aaa4d248c49768821af065f627e8bad671a961a58a  0011-ASoC-meson-add-initial-spdif-dai-support.patch
f7e1b85b59f4347918b626fd373cb23bc5ede17f52f7c8d0f29d6f80a22d28966060c7842363393505b8c059b11ee15297e3b429976a7e6e5f0189a14223a480  0012-ARM64-defconfig-enable-audio-support-for-meson-SoCs-.patch
52a9eaaf61daa1923ed91a20fc7088d532060d180755c257122b6d897a07812249fefe6f5cd568551b9b8656071be3f2f6922f34d691003d8146bb5307e7522b  0013-ARM64-dts-meson-gx-add-audio-controller-nodes.patch
5c31a60457ccc4e6fc1c6f005b9c70583a03797cd56cca3e9799d2717312fd8610ec13cd7d83eadd134379788e9655b4e95b90462076adf10ff39b71dc7c9a10  0014-snd-meson-activate-HDMI-audio-path.patch
afd396bd828a87fb001956ce7369ac8c34e51a684f7be486995463cf4d912a39b242b51af7f3a841b6ecb42ab7e4366c5f9d163992ccaf370fedd842f7a4b2c6  0015-drm-meson-select-dw-hdmi-i2s-audio-for-meson-hdmi.patch
b6ac2f340d57cd0514a8fbd54f3d17942e794f20bfe3402c64a98fdf7cca7ab487cc2bac884436324eda21482bebbdfe7633bd9f9708bc84adb720161a555b89  0016-ARM64-dts-meson-gx-add-sound-dai-cells-to-HDMI-node.patch
b55f9b3370d1c7b75c52f8f75f12a5033402d3a7f0f5be591cd6268b280a39959464570c8868e94586034f48a90f50ac17e414b191e12f7725175dca6fee2a1f  0017-ARM64-dts-meson-activate-hdmi-audio-HDMI-enabled-boa.patch
5814fcf935365b7975ae21f416122a341cef4e54dc8556a03882915d908586e42ec966b18ab5abb85b1533a1c494eeebbb237df3a4ebde28f15449bab66fa421  0018-drm-bridge-dw-hdmi-Use-AUTO-CTS-setup-mode-when-non-.patch
95da882661e07c40d726797b570b2407c94862278a2148f8ff1d3ce7ffe00ba6f83d1f06be77a1fbc59c5f1f5620c1346bd565903b49e5d657e8d143eb247af8  0019-drm-meson-Call-drm_crtc_vblank_on-drm_crtc_vblank_of.patch
8371d3c08ba0029e7c29a5171eb53ba175c0909f1a90a17967f2ca68c1d9a2dbc49a39d1102a62470d237a50a99307b7475a4ba243a0a0d6881684705f428e2b  0020-media-platform-meson-ao-cec-make-busy-TX-warning-sil.patch
896a8ba7c44744e067d3d5216d28df241c6796f1aff01b9648ea2e54e5798f25436328cdb173f00de2f998903ae01b3785a37ed07aea576ccada4590925de10f  0021-soc-amlogic-add-meson-canvas-driver.patch
ad723a68e02b2d4fd708fab3b61aba3ad888b25de9d5e73f3f00dc81bf778d1b59061aa25b840bc790903a8dc9ce180da5aca6312c9f7c8b724a18c80866e174  0022-dt-bindings-soc-amlogic-add-meson-canvas-documentati.patch
0854014aaf150468b14a739649e1461fa66450d168f3f430756bcf6d2e34158be922824d2eb62fd867e5ac28340ef120fb13504183984da7a4bd3a6fe59724b8  0023-ARM64-dts-meson-gx-add-dmcbus-and-canvas-nodes.patch
f01e6fd7a319c75c85803e1deb1681772385640fd10ef4a24b164af6e708d0d29e8118bf2be12a82e6587587a87a6b5b7331bd160c32ae9e3d5f4c1a4e62ce79  0024-drm-meson-convert-to-the-new-canvas-module.patch
783a1c4c0deb352b699faa5f572d38737c1c67983a84822b900a984e9aec521f7a762d6f139d5ce7546503af4a76fd2b604ef79a2aef5105d23fc33919fa13de  0025-WIP-drm-meson-Support-Overlay-plane-for-video-render.patch
34b404bbb4b1e2c3ae1c9fba8e297ed48d382d59208bb00340a5e9f2a86ef0f12d0fc6f347a68915cffc141e19bd4b086a1bbdc440b4bd8a1edef5a7fb79c46e  0026-media-meson-add-v4l2-m2m-video-decoder-driver.patch
608bb5d25c94e9d8e0a1ea9d9fc2eb4436870d72e39f9446322dbdc62a5b7a3fd07d735613a425e1853cf9dd3e61fc860f4310858f9cfbea62a3ba69378b653c  0027-ARM64-dts-meson-gx-add-vdec-entry.patch
0ab00106ab3a15ef884d0d08c4e947037007a4af7b03fc204346e9487f1f8841140dead1f6b8704b74f7fd955deaaa58cc7ce81c42a847bcdc5c13aac627f3e4  0001-libretech-cc-disable-CVBS-connector.patch
7715e9c35072f60856d5ab6940874f053cd5e64f09ae97c79eef98bf0856f3a27643dae1154eec787c94a8c7c84b39c580324540db37d9bf2cf70b2bfa66595a  0001-ARM64-dts-meson-add-vdec-entries.patch
6262ecd89164edcfc58ef7d50c0b027a2263e6e78f51f033b611734a8e8db0436e1b132aee65546717dd5c79635ef38c9be11cffd7808491687494df9fdcb08e  add-phicomm-n1.patch
dcc02a2e35e530490f18dd1f7e304aebd7c268ed8a46971d94e9da7dfd65b287002e6b921e46db5ce118177c9ab79fbe3640565cb2df19809b6eff514122fb43  bt-btbcm.patch
007075ca40915f20c6c8f5e04825c3433a52621deb689a9a690a3fcc618ca9629bc089a1a2ad5141fc47a8aa0aabb302ecf75e81ef8cae466e978074156231ac  brcmfmac-Disable-power-management.patch
cb2ec80ae38c4cc46ea2a4ca6d157498632f8154ef6a1dedb898c70593c812743a69a4c0d36a4e146a71c588e30e9f9b2feb415e00166850c2cf79797048b032  offset.patch
e5d214788f9a28d79a9ef135fbed3b3c87982941788ca74d31d919534d1627c1265b268982f67de6c14b331d1a38938b4c2772f17b3db3bddd3983f919fe73ca  off_error_text_offset.patch"
